<?php

if (!defined('__BACKEND__'))
    die("you cannot directly access this page");
/*
 * by SONY K Wibisono
 * e-mail : sony@sonywibisono.com
 */

//Place code here
include_once 'include/link.php';

function add_form(){
    echo "<form action=\"?mod=panel&file=link&action=add_process\" method=\"Post\">
        <table>
<tr><td>Id</td><td><input type=\"text\" name=\"id\" class=\"textbox\"/> </td></tr>
<tr><td>Link Caption</td><td><input type=\"text\" name=\"link_caption\" class=\"textbox\"/> </td></tr>
<tr><td>Link Category</td><td>";
    select_link_category($unitid);
    echo "</td></tr>
<tr><td>Url Link</td><td><input type=\"text\" name=\"url_link\" class=\"textbox\"/> </td></tr>
<tr><td>Target</td><td><input type=\"text\" name=\"target\" class=\"textbox\"/> </td></tr>
<tr><td></td><td><input type=\"submit\" value=\"Kirim\"/></tr></table></form>";
}
function list_table(){
    global $db,$page;
        $sql="select a.*,b.category_name from _link_links a,_link_category b 
        where a.link_category=b.id order by a.id asc";
        $result=$db->query($sql);
        
    echo "
<h2>Daftar Link</h2>        
<div align=\"right\"><a href=\"?mod=panel\"><img class=\"icon\" src=\"images/panel.png\" title=\"Back to Panel\"/></a>
<a href=\"?mod=panel&file=link&action=add\"><img class=\"icon\" src=\"images/panel/add_link.png\" title=\"Add Link\"/></a>        </div>
<table width=\"100%\" cellspacing=0><tr><th>No</th><th>Caption</th><th>Category</th><th>URL</th><th>Action</th></tr>";
    //;
    //for($i=0;$i<count($row);$i++){
    while ($row=$db->fetch_array($result)){  
    extract($row);
        echo "<tr><td>$id</td><td>$link_caption</td><td>$category_name</td>
    <td>$url_link</td><td><a href=\"?mod=panel&file=link&action=edit&id=$id\">
    <img class=icon src=\"images/edit.png\" title=\"Edit\" /></a>
    <a href=\"?mod=panel&file=link&action=delete&id=$id\">
    <img class=icon src=\"images/delete.png\" title=\"Hapus\" /></a></td></tr>";
    }
    echo "</table>";
}
function edit_form(){
    global $db,$id;
    $link=new linkLinks();
    $link->id=$id;
    $link->open();
    echo "
<form action=\"?mod=panel&file=link&action=edit_process\" method=\"Post\">        
<table>
<tr><td>Id</td><td><input type=\"hidden\" name=\"id\" class=\"textbox\" value=\"$id\"/> $id </td></tr>
<tr><td>Link Caption</td><td><input type=\"text\" name=\"link_caption\" class=\"textbox\"
     value=\"{$link->link_caption}\" size=40/> </td></tr>
<tr><td>Link Category</td><td>";
    select_link_category($link->link_category);
    echo "</td></tr>
<tr><td>Url Link</td><td><input type=\"text\" name=\"url_link\" class=\"textbox\" 
value=\"{$link->url_link}\" size=50/> </td></tr>
<tr><td>Target</td><td><input type=\"text\" name=\"target\" class=\"textbox\" 
value=\"{$link->target}\"/> </td></tr>
<tr><td></td><td><input type=\"submit\" value=\"Kirim\"/></tr></table></form>";
}
function delete(){
    global $id;
    $link=new linkLinks();
    $link->id=$id;
    $link->delete();
    refresh_to("?mod=panel&file=link", "Back to Link List", 2);
}
function edit_process(){
	global $id,$link_caption,$link_category,$url_link,$target;
    $link=new linkLinks();
    $link->id=$id;
    $link->link_caption=$link_caption;
    $link->link_category=$link_category;
    $link->url_link=$url_link;
    $link->target=$target;
    $link->update();
    refresh_to("?mod=panel&file=link", "Back to Link List", 2);
}

function add_process(){
    global $link_caption,$link_category,$url_link,$target;
    $link=new linkLinks();
    $link->link_caption=$link_caption;
    $link->link_category=$link_category;
    $link->url_link=$url_link;
    $link->target=$target;
    $link->insert();
    refresh_to("?mod=panel&file=link", "Back to Link List", 2);
}
switch($action){
    case 'add_process':
        add_process();
        break;
    case 'add':
        add_form();
        break;
    case 'edit':
        edit_form();
        break;
    case 'edit_process':
        edit_process();
        break;
    case 'delete':
        delete();
        break;
    default:
        list_table();
        break;
}
?>